﻿using CMS.BaseEntity;
using CMS.BusinessLogic;
using CMS.Common;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace CMS.UserControl
{
    public partial class CRUDSection : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (Session["UserId"] == null)
                {
                    Response.Redirect(Directory.HomePage);
                }
                else
                {
                    LoadGridSection();
                    txtUserName.Text = Session["UserName"].ToString();
                    if (Request.QueryString["action"] == "sedit")
                    {
                        btnUpdate.Visible = true;
                        btnSave.Visible = false;
                        BinDataToEdit();
                    }
                    else
                    {
                        btnUpdate.Visible = false;
                    }
                }
            }
        }

        ProductLogic pLogic = new ProductLogic();
        SectionLogic sLogic = new SectionLogic();
        protected void LoadGridSection()
        {
            DataTable dt = sLogic.GetListSection();
            grvSection.DataSource = dt;
            grvSection.DataBind();
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            Section section = new Section();
            section.SectionName = txtName.Text;
            section.AuthorId = int.Parse(Session["UserId"].ToString());
            section.Description = txtDescription.Text;
            section.CreateDate = txtCreateDate.Text;
            if (chkStatus.Checked == true)
                section.Enable = true;
            else
                section.Enable = false;

            if (sLogic.IsInsertSection(section))
            {
                ScriptManager.RegisterClientScriptBlock(Page, Page.GetType(), "alert", Script.Manipulate_Success, true);
                LoadGridSection();
                EmptyTextBoxes();
            }
            else
                ScriptManager.RegisterClientScriptBlock(Page, Page.GetType(), "alert", Script.Manipulate_Fail, true);
        }

        protected void btnClear_Click(object sender, EventArgs e)
        {
            txtName.Text = string.Empty;
            txtDescription.Text = string.Empty;
            txtName.Focus();
            //Response.Redirect("ChannelProduct.aspx");
        }

        public void EmptyTextBoxes()
        {
            txtName.Text = string.Empty;
            txtDescription.Text = string.Empty;
            txtName.Focus();
        }

        public void BinDataToEdit()
        {
            Section section = new Section();
            section.SectionId = int.Parse(Request.QueryString["sid"].ToString());
            DataTable dt = sLogic.GetSectionById(section);
            DataRow dr = dt.Rows[0];
            txtName.Text = dr["SectionName"].ToString();
            txtDescription.Text = dr["Description"].ToString();
            txtCreateDate.Text = DateTime.Now.ToString();
            txtName.Focus();
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            Section section = new Section();
            section.SectionId = int.Parse(Request.QueryString["sid"].ToString());
            section.SectionName = txtName.Text;
            section.AuthorId = int.Parse(Session["UserId"].ToString());
            section.Description = txtDescription.Text;
            section.LastModify = DateTime.Now.ToString();
            if (chkStatus.Checked == true)
                section.Enable = true;
            else
                section.Enable = false;

            if (sLogic.IsUpdateSection(section))
            {
                ScriptManager.RegisterClientScriptBlock(Page, Page.GetType(), "alert", Script.Manipulate_Success, true);
                LoadGridSection();
                EmptyTextBoxes();
            }
            else
                ScriptManager.RegisterClientScriptBlock(Page, Page.GetType(), "alert", Script.Manipulate_Fail, true);
        }

        protected void grvSection_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            Section section = new Section();
            section.SectionId = int.Parse(grvSection.DataKeys[e.RowIndex].Value.ToString());
            if (sLogic.IsDeleteSection(section))
            {
                ScriptManager.RegisterClientScriptBlock(Page, Page.GetType(), "alert", Script.Manipulate_Success, true);
                LoadGridSection();
                EmptyTextBoxes();
            }
            else
                ScriptManager.RegisterClientScriptBlock(Page, Page.GetType(), "alert", Script.Manipulate_Fail, true);
        }
    }
}